package io.kiki.stack.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * Protocol code based newEncoder, the main newEncoder for a certain protocol, which is lead by one or multi bytes (magic code).
 * <p>
 * Notice: this is stateless can be noted as {@link io.netty.channel.ChannelHandler.Sharable}
 */

@Slf4j
@Setter
@Getter
@ChannelHandler.Sharable
public class MessageToByteEncoder extends io.netty.handler.codec.MessageToByteEncoder<Serializable> {


    public MessageToByteEncoder() {
        super();

    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Serializable msg, ByteBuf out) throws Exception {

        Protocol protocol = ProtocolImpl.protocol;
        protocol.getEncoder().encode(channelHandlerContext, msg, out);
    }

}
